const CONFIG = require("../../config")
const knex = require('knex')(CONFIG.mysql);
module.exports = {
    /*
    如何获取值 get请求方法   获取req的query方法取值
               post请求方法  获取req的body方法取值
    */
    async getList(req, res) {
        //获取url值
        const { current, size } = req.query;
        //console.log("-------", current, size);
        let data;
        knex('blade_visual_map').select("id", "name").then(resp => {
            //console.log(resp)
            data = resp;
        }).then(() => {
            if (data.length > 0) {
                items = {
                    code: 200,
                    data: {
                        current: current,
                        hitCount: false,
                        orders: [],
                        pages: 1,
                        records: data,
                        searchCount: true,
                        size: size,
                        total: 30
                    },
                    msg: "操作成功",
                    success: true
                }
                res.send(items)
            } else {
                res.send("error");
            }
        })
    },

    async getDetail(req, res) {
        const { id } = req.query;
        //console.log("-------", current, size);
        let data;
        knex('blade_visual_map').select().where({ id: id }).then(resp => {
            //console.log(resp[0])
            data = resp[0];
        }).then(() => {
            if (data) {
                items = {
                    code: 200,
                    data: data,
                    msg: "操作成功",
                    success: true
                }
                res.send(items)
            } else {
                res.send("error");
            }
        })
    },

    async postSave(req, res) {
        let { data, name } = req.body;

        knex('blade_visual_map').insert({ data: data, name: name }).then(resp => {
                console.log("res", resp);
                if (resp) {
                    res.send({
                        code: 200,
                        msg: "操作成功",
                        success: true
                    })
                } else {
                    res.send({
                        code: 4001,
                        msg: "操作失败",
                        success: true
                    })
                }
            })
            //res.send('postUpdate');
    },

    async postUpdate(req, res) {
        let { name, data, id } = req.body;
        //console.log("---------------", req.body)

        knex('blade_visual_map').update({ name: name, data: data }).where({ id: id }).then(resp => {
            console.log(resp);
            if (resp === 1) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },

    async postRemove(req, res) {
        console.log("res._parsedUrl", req.query.ids);
        const ids = req.query.ids;

        knex('blade_visual_map').delete().where({ id: ids }).then(resp => {
            console.log(resp);
            if (resp) {
                res.send({
                    code: 200,
                    msg: "操作成功",
                    success: true
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
    async getData(req, res) {
        //console.log("res._parsedUrl", req.query.id);
        let id = req.query.id;
        knex('blade_visual_map').select().where({ id: id }).then(resp => {
            //对象字符串格式处理成数组
            let data = eval("(" + resp[0].data + ")");
            //console.log(data.features);
            if (resp) {
                res.send({
                    features: data.features,
                    type: data.type
                })
            } else {
                res.send({
                    code: 4001,
                    msg: "操作失败",
                    success: true
                })
            }
        })
    },
}